package com.hndzyu.yd.customtask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.vip.GameActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/11/8.
 */
public class ScreenshotFragment extends Fragment {
    TextView topTitle;
    RecyclerView recycler;
    SmartRefreshLayout srl;
    private ScreenAdapter mAdapter;
    private MyTaskAdapter mTaskAdapter;
    private View nodata;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_screenshot, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final ImageView iv_zhengzaicanyu = view.findViewById(R.id.iv_zhengzaicanyu);
        final ImageView iv_kecanyu = view.findViewById(R.id.iv_kecanyu);
        final TextView tv_zhengzaicanyu = view.findViewById(R.id.tv_zhengzaicanyu);
        final TextView tv_kecanyu = view.findViewById(R.id.tv_kecanyu);
        nodata = view.findViewById(R.id.nodata);
        view.findViewById(R.id.rl_fragment_ge_task_list).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_kecanyu.setImageResource(R.mipmap.kcy_s);
                iv_zhengzaicanyu.setImageResource(R.mipmap.jxz);
                tv_zhengzaicanyu.setTextColor(getResources().getColor(R.color.tv_selector2));
                tv_kecanyu.setTextColor(getResources().getColor(R.color.tv_selector));
                taskend();
                recycler.setAdapter(mAdapter);
            }
        });
        view.findViewById(R.id.rl_zhengzaicanyuarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_kecanyu.setImageResource(R.mipmap.kcy);
                iv_zhengzaicanyu.setImageResource(R.mipmap.jxz_s);
                tv_zhengzaicanyu.setTextColor(getResources().getColor(R.color.tv_selector));
                tv_kecanyu.setTextColor(getResources().getColor(R.color.tv_selector2));
                approval();
                recycler.setAdapter(mTaskAdapter);
            }
        });
        recycler = view.findViewById(R.id.recycler);
        srl = view.findViewById(R.id.srl);
        srl.setRefreshHeader(new MaterialHeader(mcontext));
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                iv_kecanyu.setImageResource(R.mipmap.kcy_s);
                iv_zhengzaicanyu.setImageResource(R.mipmap.jxz);
                tv_zhengzaicanyu.setTextColor(getResources().getColor(R.color.tv_selector2));
                tv_kecanyu.setTextColor(getResources().getColor(R.color.tv_selector));
                taskend();
                recycler.setAdapter(mAdapter);
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(mcontext));
        mAdapter = new ScreenAdapter(mcontext);
        recycler.setAdapter(mAdapter);
        mTaskAdapter = new MyTaskAdapter(mcontext);
        view.findViewById(R.id.ll_kecanyu).setSelected(true);
        view.findViewById(R.id.banner_task).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mcontext, GameActivity.class));
            }
        });
        taskend();
        setListener();
    }

    private void setListener() {
        mAdapter.setScreenItemListener(new ScreenAdapter.ScreenItemListener() {
            @Override
            public void onItemClick(ScreenBean.DataBean item) {
                if (item.getType() == 4) {
//                    Toast.makeText(mcontext, "信用卡", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), CreditCardDetailsActivity.class).putExtra("taskid", item.getTaskid()).putExtra("Detailtype", 4));
                } else {
                    startActivity(new Intent(getActivity(), CreditTaskDetailsActivity.class).putExtra("taskid", item.getTaskid()));

                }
            }
        });
        mTaskAdapter.setMyTaskItemListener(new MyTaskAdapter.MyTaskItemListener() {
            @Override
            public void onItemClick(ScreenBean.DataBean item) {
                if (item.getType() == 4) {
//                    Toast.makeText(mcontext, "信用卡", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), CreditCardDetailsActivity.class).putExtra("taskid", item.getTaskid()).putExtra("Detailtype", 4));
                } else {
                    startActivity(new Intent(getActivity(), CreditTaskDetailsActivity.class).putExtra("taskid", item.getTaskid()));

                }
            }
        });
    }

    private void taskend() {
        OkHttpDownloadJsonUtil.downloadJson(mcontext, Path.TASKEND(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                screen();
            }
        });
    }

    private void approval() {
        OkHttpDownloadJsonUtil.downloadJson(mcontext, Path.APPROVAL(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                MyTask();
            }
        });
    }

    private void screen() {
        OkHttpDownloadJsonUtil.downloadJson(mcontext, Path.subTaskList + "?uid=" + UserBean.uid + "&status=0", new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                    @Override
                    public void onsendJson(String json) {
                        srl.finishRefresh();
                        srl.finishLoadMore();
                        if (!"1".equals(json) && !"0".equals(json)) {
                            try {
                                Log.d("SHDJKSHDS", json);
                                Gson gson = new Gson();
                                ScreenBean screenBean = gson.fromJson(json, ScreenBean.class);
                                List<ScreenBean.DataBean> data = screenBean.getData();
                                List<ScreenBean.Data1Bean> data1 = screenBean.getData1();
                                ArrayList<ScreenBean.DataBean> list = new ArrayList<>();
                                for (ScreenBean.Data1Bean b : data1) {
                                    list.add(new ScreenBean.DataBean(b.getTaskid(), b.getLogo(), b.getType(), b.getTitle(), b.getAcount(), b.getIsvip(), -1));
                                }
                                list.addAll(data);
                                if (list.size() > 0) {
                                    mAdapter.updateData(list);
                                    nodata.setVisibility(View.GONE);
                                    recycler.setVisibility(View.VISIBLE);
                                } else {
                                    nodata.setVisibility(View.VISIBLE);
                                    recycler.setVisibility(View.GONE);
                                }
                            } catch (Exception e) {
                                nodata.setVisibility(View.VISIBLE);
                                recycler.setVisibility(View.GONE);
                                Toast.makeText(mcontext, "数据异常", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(mcontext, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
        );
    }

    private void MyTask() {
        OkHttpDownloadJsonUtil.downloadJson(mcontext, Path.subTaskList + "?uid=" + UserBean.uid + "&status=1", new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                Log.d("SHDJKSHDSS", json);
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"0".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        ScreenBean screenBean = gson.fromJson(json, ScreenBean.class);
                        List<ScreenBean.DataBean> data = screenBean.getData();
                        List<ScreenBean.Data1Bean> data1 = screenBean.getData1();
                        ArrayList<ScreenBean.DataBean> list = new ArrayList<>();
                        for (ScreenBean.Data1Bean b : data1) {
                            list.add(new ScreenBean.DataBean(b.getTaskid(), b.getLogo(), b.getType(), b.getTitle(), b.getAcount(), b.getIsvip(), -1, b.getStatus(), b.getLiuyan()));
                        }
                        list.addAll(data);
                        if (list.size() > 0) {
                            mTaskAdapter.updateData(list);
                            nodata.setVisibility(View.GONE);
                            recycler.setVisibility(View.VISIBLE);
                        } else {
                            nodata.setVisibility(View.VISIBLE);
                            recycler.setVisibility(View.GONE);
                        }
                    } catch (Exception e) {
                        Toast.makeText(mcontext, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(mcontext, "网络异常", Toast.LENGTH_SHORT).show();
                }

            }

        });
    }
    Context mcontext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }
}
